package cn.com.hongfu.controller;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import cn.com.hongfu.model.Case;
import cn.com.hongfu.model.CaseImg;
import cn.com.hongfu.service.CaseService;
import cn.com.hongfu.service.InfoSetService;

@Controller
@RequestMapping("case")
public class CaseController {
	@Autowired
	public CaseService caseService ;
	@Autowired
	public InfoSetService infoSetService ;
	@RequestMapping("addUI")
	public String addUI(ModelMap modelMap){
		modelMap.addAttribute("styles", infoSetService.getInfoItemByInfoSetName("style"));
		System.out.println(infoSetService.getInfoItemByInfoSetName("style"));
		modelMap.addAttribute("houseTypes", infoSetService.getInfoItemByInfoSetName("houseType"));
		return "/case/caseAdd";
	}
	@RequestMapping("add")
	public String add(HttpServletRequest request){
		String houseName = request.getParameter("houseName");
		String size = request.getParameter("size");
		String houseType = StringUtils.isEmpty(request.getParameter("houseType"))==true?"0":request.getParameter("houseType");
		String style =StringUtils.isEmpty(request.getParameter("style"))==true?"0":request.getParameter("style") ;
		String budget = request.getParameter("budget");
		String cDesc = request.getParameter("cDesc");
		Case c = new Case();
		c.setHouseName(houseName);
		c.setHouseType(Integer.parseInt(houseType));
		c.setStyle(Integer.parseInt(style));
		c.setcDesc(cDesc);
		c.setBudget(budget);
		c.setSize(size);
		caseService.save(c);
		return "redirect:/case/list.html";
	}
	@RequestMapping("list")
	public String list(ModelMap modelMap){
		modelMap.addAttribute("cs", caseService.getAll());
		return "/case/caseList";
	}
	@RequestMapping("caseImgList")
	public String caseImgList(ModelMap modelMap,HttpServletRequest request){
		int caseId = Integer.parseInt(request.getParameter("caseId"));
		modelMap.addAttribute("imgs", caseService.getImgByCaseId(caseId));
		modelMap.addAttribute("caseId", request.getParameter("caseId"));
		return "/case/caseImgList";
	}
	@RequestMapping("caseImgAdd")
	public String caseImgAdd(ModelMap modelMap,HttpServletRequest request,@RequestParam(value = "file", required = false) MultipartFile file){
		int caseId = Integer.parseInt(request.getParameter("caseId"));
		int imgOrder = Integer.parseInt(request.getParameter("imgOrder"));
		String path = request.getSession().getServletContext().getRealPath("upload");  
        String fileName = file.getOriginalFilename();  
        File targetFile = new File(path, fileName);  
        if(!targetFile.exists()){  
            targetFile.mkdirs();  
        }  
        try {  
            file.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        CaseImg caseImg = new CaseImg();
		caseImg.setCaseId(caseId);
		caseImg.setSrc(request.getContextPath()+"/upload/"+fileName);
		caseImg.setImgOrder(imgOrder);
		caseService.saveImg(caseImg);
		modelMap.addAttribute("caseId", caseId);
		return "redirect:/case/caseImgList.html";
	}
}
